package com.fottas.adware.payment.action;

import com.fottas.adware.common.api.IErrorCode;
import com.fottas.adware.common.emun.PayTypeEnum;
import com.fottas.adware.common.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@Service
public class PayActionFactory {
    @Autowired
    private PayAction[] payActions;

    @Nonnull
    public PayAction getPayAction(@Nullable Integer type) {
        PayTypeEnum payType = PayTypeEnum.getPayActionByType(type);
        for (PayAction payAction : payActions) {
            if (payAction.getPayType() == payType) {
                return payAction;
            }
        }
        throw ApiException.create(IErrorCode.PAY_TYPE_FAILED);
    }
}
